package com.kedou.sdk.ui.assist;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.kedou.sdk.params.bean.SimpleUserInfo;
import com.kedou.sdk.utils.ResUtils;

import java.util.ArrayList;
import java.util.List;

public class AccountChangeListAdapter extends BaseAdapter {

    private final List<SimpleUserInfo> mChannels = new ArrayList<>();
    private LayoutInflater inflater;
    private boolean isFirstLoadView;
    private int lastSelectedIndex;

    public void setAdapterData(List<SimpleUserInfo> data) {
        this.mChannels.clear();
        this.mChannels.addAll(data);
        this.isFirstLoadView = true;
    }

    public void setLastSelectedIndex(int position) {
        this.lastSelectedIndex = position;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mChannels.size();
    }

    @Override
    public SimpleUserInfo getItem(int position) {
        return mChannels.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Context context = parent.getContext();
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();

            if (inflater == null) {
                inflater = LayoutInflater.from(context);
            }

            final int layoutResId = ResUtils.getLayoutIdentifier(context, "hnssn_item_account_change");
            final int timeResId = ResUtils.getViewIdentifier(context, "hnssn_item_account_time");
            final int titleResId = ResUtils.getViewIdentifier(context, "hnssn_item_account_name");
            final int checkResId = ResUtils.getViewIdentifier(context, "hnssn_item_account_checkbox");

            convertView = inflater.inflate(layoutResId, parent, false);
            viewHolder.account = convertView.findViewById(titleResId);
            viewHolder.time = convertView.findViewById(timeResId);
            viewHolder.checked = convertView.findViewById(checkResId);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        SimpleUserInfo channel = getItem(position);
        if (channel != null) {
            viewHolder.account.setText(channel.getUsername());

        }
        String loginTime = channel.getLogintime();
        if(loginTime.contains(" ")){
            loginTime =loginTime.split(" ")[0];
        }
//        viewHolder.time.setText("上次登录："+loginTime);
        viewHolder.time.setVisibility(position==0?View.VISIBLE:View.GONE);
        viewHolder.checked.setChecked(position == lastSelectedIndex);
        viewHolder.account.setSelected(position == lastSelectedIndex);
        if (isFirstLoadView) {
            isFirstLoadView = false;
        }

        return convertView;
    }

    public static class ViewHolder {
        public TextView account;
        public TextView time;
//        public ImageView icon;
        public CheckBox checked;
    }

}
